const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes} = require("../../models/db");

// 保存送货路线区域
router.post('/deliver/getBuIdByName', async ctx => {
    let tenantId = ctx.user.tenantId;
    let name = '零售客户'
    let contact = await db.models['contact'].findOne({
        where: {
            tenantId,
            name,
            isCustomer: 1,
        }
    })
    let location = await db.models['storage'].findOne({
        where: {
            tenantId,
            name: '默认仓库'
        }
    })
    if(contact) {
        return ctx.suc('', {id: contact.id,name: contact.name,locationId: location.id, locationName: location.name})
    }
    else {
        let obj = {
            tenantId,
            name,
            pinYin: 'lskh',
            isCustomer: 1,
        }
        contact = await db.models['contact'].create(obj)
        return ctx.suc('', {id: contact.id,name: contact.name,locationId: location.id, locationName: location.name})
    }
})

module.exports = router;
